package com.example.forum.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

/**
 * 统一返回数据格式
 * @param <T>
 */
@Data
public class AppResult <T> {
    @JsonInclude(JsonInclude.Include.ALWAYS) // 不论任何情况都参与JSON序列化
    private int code;

    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String message;

    @JsonInclude(JsonInclude.Include.ALWAYS)
    private T data;//   T泛型会在编译时变成 Object 类

    /**
     * 封装成功方法
     */
    public static AppResult success() {
        return new AppResult(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage());
    }
    public static <T> AppResult success(T data) {
        return new AppResult<>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(),data);
    }
    public static <T> AppResult success(String message,T data) {
        return new AppResult<>(ResultCode.SUCCESS.getCode(), message,data);
    }

    /**
     * 封装失败方法
     */
    public static AppResult failed() {
        return new AppResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage());
    }
    public static AppResult failed(String message) {
        return new AppResult(ResultCode.FAILED.getCode(),message);
    }
    //  @Param data：异常描述信息
    public static <T>AppResult failed(T data) {
        return new AppResult(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage(),data);
    }
    public static AppResult failed(ResultCode resultCode) {
        return new AppResult(resultCode.getCode(),resultCode.getMessage());
    }



    public AppResult() {}
    public AppResult(int code,String message) {
        this(code,message,null);
    }
    public AppResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }



}
